package com.xci.sys.api;

import com.xci.core.annotation.AllowAnonymous;
import com.xci.core.annotation.Authorize;
import com.xci.core.base.ApiController;
import com.xci.core.base.RestMessage;
import com.xci.core.domain.BoolMessage;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.internal.Const;
import com.xci.sys.entity.SysApp;
import com.xci.sys.filter.AppFilter;
import com.xci.sys.service.AppService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 应用接口
 * @author 吕艳阳
 */
@ApiSort(4)
@Api(tags = "应用接口")
@Authorize
@RestController
@RequestMapping(value = "/api/sys/app", produces = Const.P_JSON)
public class AppApiController extends ApiController {
    @Resource
    private AppService appService;//应用服务

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新建应用")
    @Authorize(code = "sys.app.insert")
    @PostMapping("/insert")
    public RestMessage insert(@RequestBody SysApp entity) {
        return appService.insert(entity);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改应用")
    @Authorize(code = "sys.app.update")
    @PostMapping("/update")
    public RestMessage update(@RequestBody SysApp entity) {
        return appService.update(entity);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "修改应用状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "应用主键字符串", required = true),
            @ApiImplicitParam(name = "status", value = "应用状态", required = true)
    })
    @Authorize(code = "sys.app.updateStatus")
    @PostMapping("/updateStatus")
    public RestMessage updateStatus(String ids, Boolean status) {
        return appService.updateStatus(ids, status);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除应用")
    @ApiImplicitParam(name = "ids", value = "应用主键字符串", required = true)
    @Authorize(code = "sys.app.delete")
    @PostMapping("/delete")
    public RestMessage delete(String ids) {
        return appService.delete(ids);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "检查应用主键是否存在")
    @ApiImplicitParam(name = "id", value = "应用主键", required = true)
    @PostMapping("/existById/{id}")
    public RestMessage existById(@PathVariable String id) {
        return RestMessage.result(appService.existById(id));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "检查应用名称是否存在")
    @ApiImplicitParam(name = "name", value = "应用名称", required = true)
    @PostMapping("/existByName")
    public RestMessage existByName(String name) {
        return RestMessage.result(appService.existByName(name));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "查询单个应用")
    @ApiImplicitParam(name = "id", value = "应用主键", required = true)
    @PostMapping("/selectById/{id}")
    public RestMessage<SysApp> selectById(@PathVariable String id) {
        return RestMessage.success(appService.selectById(id));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "查询应用列表")
    @Authorize(code = "sys.app.select")
    @PostMapping("/selectList")
    public RestMessage<List<SysApp>> selectList(AppFilter filter) {
        return RestMessage.success(appService.selectList(filter));
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "导出应用列表")
    @Authorize(code = "sys.app.export")
    @PostMapping(value = "/export", produces = {Const.P_OCTET, Const.P_JSON})
    public void export(AppFilter filter) {
        ExcelHelper.exportWeb(appService.selectList(filter), SysApp.class, "系统应用列表");
    }

    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "刷新应用缓存")
    @AllowAnonymous
    @PostMapping(value = "/refreshCache")
    public RestMessage refreshCache() {
        appService.refreshCache();
        return RestMessage.success();
    }
}